package com.gitee.qdbp.general.trade.biz.order.main.service;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.gitee.qdbp.able.exception.ServiceException;
import com.gitee.qdbp.able.jdbc.ordering.OrderPaging;
import com.gitee.qdbp.able.jdbc.paging.PageList;
import com.gitee.qdbp.general.trade.api.order.main.model.OrderFormBean;
import com.gitee.qdbp.general.trade.api.order.main.model.OrderFormDetails;
import com.gitee.qdbp.general.trade.api.order.main.model.OrderFormWhere;
import com.gitee.qdbp.general.trade.api.order.main.model.OrderItemBean;
import com.gitee.qdbp.general.trade.api.order.main.model.OrderItemWhere;
import com.gitee.qdbp.general.trade.api.order.main.service.IOrderMainQueryer;
import com.gitee.qdbp.general.trade.biz.order.main.basic.OrderFormBasic;
import com.gitee.qdbp.general.trade.biz.order.main.basic.OrderItemBasic;

/**
 * 订单主业务处理类
 *
 * @author zhh
 * @version 180622
 */
@Service
@Transactional(readOnly = true)
public class OrderMainQueryer implements IOrderMainQueryer {

    @Autowired
    private OrderFormBasic orderFormBasic;
    @Autowired
    private OrderItemBasic orderItemBasic;

    @Override
    public OrderFormBean find(String id) throws ServiceException {
        return orderFormBasic.findById(id);
    }

    @Override
    public OrderFormBean find(OrderFormWhere where) throws ServiceException {
        return orderFormBasic.find(where);
    }

    @Override
    public PageList<OrderFormBean> list(OrderFormWhere where, OrderPaging paging) throws ServiceException {
        return orderFormBasic.list(where, paging);
    }

    @Override
    public List<OrderItemBean> listItems(String orderId) throws ServiceException {
        OrderItemWhere where = new OrderItemWhere();
        where.setOrderId(orderId);
        PageList<OrderItemBean> temp = orderItemBasic.list(where, OrderPaging.NONE);
        return temp == null ? null : temp.toList();
    }

    @Override
    public PageList<OrderItemBean> listItems(OrderItemWhere where, OrderPaging paging) throws ServiceException {
        return orderItemBasic.list(where, paging);
    }

    @Override
    public PageList<OrderFormDetails> listDetails(OrderFormWhere where, OrderPaging paging) throws ServiceException {
        // 查询订单列表
        PageList<OrderFormBean> paged = orderFormBasic.list(where, paging);

        PageList<OrderItemBean> items;
        { // 查询订单明细
            OrderItemWhere w = new OrderItemWhere();
            for (OrderFormBean form : paged) {
                w.addOrderId(form.getId());
            }
            items = orderItemBasic.list(w, OrderPaging.NONE);
        }

        // 组合
        PageList<OrderFormDetails> result = PageList.of(OrderFormBean.to(paged.toList(), OrderFormDetails.class));
        for (OrderFormDetails detail : result) {
            String orderId = detail.getId();
            for (OrderItemBean item : items) {
                if (orderId.equals(item.getOrderId())) {
                    detail.addItem(item);
                }
            }
        }

        return result;
    }
}
